package com.njupt.Array;

import java.util.Arrays;

public class ProductExceptSelf_238 {


    public int[] productExceptSelf(int[] nums) {

        int[] L = new int[nums.length];
        L[0] = 1;
        int[] R = new int[nums.length];
        R[nums.length - 1] = 1;

        for(int i = 1;i < L.length;i++){
            L[i] = L[i -1] * nums[i - 1];
        }

        for(int i = nums.length - 2;i >=0;i--){
            R[i] = R[i + 1] * nums[i + 1];
        }

        for(int i = 0;i < nums.length;i++){
//            if(i == 0){
//                nums[i] = R[i];
//            } else if (i == nums.length -1) {
//                nums[i] = L[i];
//            }else {
//                nums[i] = L[i] * R[i];
//            }

            nums[i] = L[i] * R[i];
        }
        return nums;
    }

    public static void main(String[] args) {
        int[] nums = {1,2,3,4};
        ProductExceptSelf_238 test = new ProductExceptSelf_238();
        int[] ints = test.productExceptSelf(nums);
        for(int i = 0 ;i < ints.length;i++){
            System.out.print(ints[i] + " ");
        }
    }
}
